import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要查找的文件路径:");
        String s = scanner.next();
        File file = new File(s);

        if(!file.exists()) {
            System.out.println("路径文件不存在!");
            return;
        }

        if(!file.isDirectory()) {
            System.out.println("路径文件不是目录!");
            return;
        }


        System.out.println("请输入要查找的文件或文件内容:");
        String s1 = scanner.next();
        scanDir(file,s1);
    }

    private static void scanDir(File file, String s1) {
        File[] files = file.listFiles();

        if(files == null) {
            return;
        }

        for (File f : files) {
            if(f.isDirectory()) {
                scanDir(f,s1);
            }else {
                tiDelete(f,s1);
            }
        }
    }

    private static void tiDelete(File f, String s1) {
        if(f.getName().contains(s1)) {
            try {
                System.out.println("查询到以下文件:" + f.getCanonicalPath());
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        try(InputStream inputStream = new FileInputStream(f)) {
            StringBuilder stringBuilder = new StringBuilder();
            Scanner scanner = new Scanner(inputStream);

            while(scanner.hasNextLine()) {
                stringBuilder.append(scanner.nextLine());
            }

           if(stringBuilder.indexOf(s1) >= 0) {
               System.out.println("查询到以下内容文件:" + f.getCanonicalPath());
               return;
           }

        }catch(IOException e) {
            e.printStackTrace();
        }
    }
}
